using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;

namespace Boids2D
{
    class Main
    {
        DXForm form;
        World myWorld;
        System.Windows.Forms.Timer timer;
        Device device;

        public DXForm Form
        {
            get { return form; }
            set { form = value; }
        }


        public Main()
        {
            form = new DXForm();
            myWorld = new World(form.DxPanel.Width, form.DxPanel.Height);



            form.GoButtonClicked += new EventHandler(form_GoButtonClicked);

            form.TrackBarAvoidMouse.ValueChanged += new EventHandler(TrackBarAvoidMouse_ValueChanged);
            form.TrackBarKeepAway.ValueChanged += new EventHandler(TrackBarKeepAway_ValueChanged);
            form.TrackBarMatchVelocity.ValueChanged += new EventHandler(TrackBarMatchVelocity_ValueChanged);
            form.TrackBarMoveToCenter.ValueChanged += new EventHandler(TrackBarMoveToCenter_ValueChanged);
            form.TrackBarSenseDistance.ValueChanged += new EventHandler(TrackBarSenseDistance_ValueChanged);
            form.TrackBarSenseAngle.ValueChanged += new EventHandler(TrackBarSenseAngle_ValueChanged);

            form.TrackBarMoveToCenter.Value =14;
            form.TrackBarKeepAway.Value = 1;
            form.TrackBarMatchVelocity.Value = 34;
            form.TrackBarAvoidMouse.Value = 5;
            form.TrackBarSenseDistance.Value = 100;
            form.TrackBarSenseAngle.Value = 33;

            form.DxPanel.MouseMove += new MouseEventHandler(DxPanel_MouseMove);
            form.DxPanel.MouseEnter += new EventHandler(DxPanel_MouseEnter);
            form.DxPanel.MouseLeave += new EventHandler(DxPanel_MouseLeave);
            
            
            device = form.DxPanel.Device;
            myWorld.Draw(device);
            device.Clear(ClearFlags.Target, Color.White, 1.0f, 0);
            
            form.DxPanel.Invalidate();
device.Present();


            timer = new System.Windows.Forms.Timer();
            timer.Interval = 16;
            timer.Tick += new EventHandler(timer_Tick);


        }

        void TrackBarSenseAngle_ValueChanged(object sender, EventArgs e)
        {
            myWorld.SenseAngle = form.TrackBarSenseAngle.Value / 100.0 * Math.PI;
        }

        void TrackBarSenseDistance_ValueChanged(object sender, EventArgs e)
        {
            myWorld.SenseDistance = form.TrackBarSenseDistance.Value;
        }

        void TrackBarMoveToCenter_ValueChanged(object sender, EventArgs e)
        {
            myWorld.MoveToCenterScaler = form.TrackBarMoveToCenter.Value;
        }

        void TrackBarMatchVelocity_ValueChanged(object sender, EventArgs e)
        {
            myWorld.MatchVelocityScaler = form.TrackBarMatchVelocity.Value;
        }

        void TrackBarKeepAway_ValueChanged(object sender, EventArgs e)
        {
            myWorld.KeepAwayScaler = form.TrackBarKeepAway.Value;
        }

        void TrackBarAvoidMouse_ValueChanged(object sender, EventArgs e)
        {
            myWorld.AvoidScaler = form.TrackBarAvoidMouse.Value;
        }

        void DxPanel_MouseLeave(object sender, EventArgs e)
        {
            myWorld.AvoidingLocation = false;
        }

        void DxPanel_MouseEnter(object sender, EventArgs e)
        {
            myWorld.AvoidingLocation = true;
        }

        void DxPanel_MouseMove(object sender, MouseEventArgs e)
        {
            myWorld.AvoidLoc = new Vector2((float)e.X, (float)e.Y);
            //myWorld.AvoidLoc.X = e.X;
            //myWorld.AvoidLoc.Y = e.Y;
        }

        void form_GoButtonClicked(object sender, EventArgs e)
        {
            timer.Enabled = !timer.Enabled;
        }
        void timer_Tick(object sender, EventArgs e)
        {
            myWorld.UpdateWorld();
            myWorld.Draw(device);
            form.DxPanel.Refresh();
            /*
            dxPanel.MeshList.Clear();

            Vector2 centerOfMass = GetCenterOfMass();
            Vector2 aveVelocity = GetAverageVelocity();
            foreach (Boid myBoid in boidList)
            {
                UpdateBoid(myBoid, centerOfMass, aveVelocity);
                AddBoidToMeshList(myBoid);
            }
            dxPanel.Refresh();
            */
        }
    }
}
